//index.js
//获取应用实例
const app = getApp()
const quote = app.globalData.quote;
const path = quote.paths;
const localData = quote.localData;
var template = require('../../../template/template.js');
// const util = require('../../../utils/util.js');
// const tools = require('../../../../utils/tools.js');
const api = require('../../../utils/api.js');
Page({
 
  data: {
    indexData:[{}],
    hidePop: true,
    openid:'',
    taskList:[],
    IsAuthorization:null,
    TasksCompletedCount:null,
    taskNumber_show:false,
    UCUID:null,
  },

  onLoad: function (options) {

     var that = this 
    
    wx.getStorage({
      key: 'UCUID',
      success: function (res) {
        
        console.log('获取UCUID成功1', res)
        let dataDic = {
          UCUID: res.data
        };
        api.methodEnglishCorner_RecordLanding({
          dataDic,
          success: function (res) {
            console.log(res.data, '39记录登陆任务')
          }
        })
        app.globalData.UCUID = res.data
        that.setData({
          UCUID:res.data
        })
        that.taskPrompt()
        
      },
      fail: function (res) {
        console.log('获取UCUID失败1', res)

      }
    })
    wx.getStorage({
      key: 'IsAuthorization',
      success: function (res) {
        console.log('获取IsAuthorization成功1', res)
        if (res.data == true) {
          console.log('32')
          that.setData({         
            taskNumber_show: true
          })
   
        }
        that.setData({
          IsAuthorization:res.data
        })
        app.globalData.IsAuthorization = res.data

      },
      fail: function (res) {
        console.log('获取IsAuthorization失败', res)

      }
    })
     console.log(options,'查看分享用户点击参数')
    var shareID = options.shareID
    var UID = options.UCUID
    if (shareID == 1){
      let dataDic = {
        TaskId:10,
        UCUID: app.globalData.UCUID
      };
      api.methodEnglishCorner_RecordingTask({
        dataDic,
        success:function(res){
          console.log(res,'38任务记录是否成功')
        }
      })
    }

    //调用登录接口
    wx.login({
      success: (data) => {

        app.globalData.wxCode = data.code;

        let dataDic = {
          appid: app.globalData.appId,
          secret: app.globalData.appSecret,
          js_code: data.code,
        };

        // 通过传code给服务器，调用微信接口获取用户openid
        api.methodRequestWechatAPI({
          dataDic,
          success: (res) => {
            if (res.data.code != 0) {
            } else {
              console.log(res.data.data.openid, '46')
              let dataDic = {
                OpenId: res.data.data.openid
              }
              api.methodEnglishCorner_Task({
                dataDic,
                success: function (res) {
                  console.log(res.data.data.TaskList, '任务列表')
                  that.setData({
                    taskList:res.data.data.TaskList
                  })
                }
              })
             
           
              console.log(this.data.openid, '51', this.data.taskList)
             
 
            }
          },    
        });
      },
 
    });
    wx.showShareMenu({
      withShareTicket: true,
      success: function (res) {
        // 分享成功
        console.log('练习页面分享成功', res)
      },
      fail: function (res) {
        // 分享失败
        console.log(res)
      }
    })
  // this.taskList()
   
    app.globalData.template.tabbar("tabBar", 0, this)//0表示第一个tabbar
    

  },
   //任务小红点

  taskPrompt:function(){
   var that = this 
    if (app.globalData.UCUID == null) {
      that.setData({
        UCUID: 1
      })
    } else {
      that.setData({
        UCUID: app.globalData.UCUID
      })
    }
    let dataDic = {
      UCUID: that.data.UCUID
    };

    api.methodEnglishCorner_HomeIndex({
      dataDic,
      success:function(res){
        console.log(res,'142任务小红点数量')
        if (res.data.data.TasksCompletedCount == 0){
            that.setData({
              taskNumber_show:false,
            })
        }
        that.setData({
          TasksCompletedCount: res.data.data.TasksCompletedCount
        })
      }
    })
  },
  onShow:function(){
    var that = this 
   // console.log(options, '查看分享用户点击参数2')
    if (app.globalData.IsAuthorization == true) {
      that.setData({

        taskNumber_show: false
      })
      let dataDic = {
        UCUID: app.globalData.UCUID
      };
      api.methodEnglishCorner_RecordLanding({
        dataDic,
        success: function (res) {
          console.log(res.data, '114记录登陆任务')
        }
      })
    }
  
    wx.getStorage({
      key: 'IsAuthorization',
      success: function (res) {
        console.log('获取IsAuthorization成功', res)
      
        app.globalData.IsAuthorization = res.data
        
      },
      fail: function (res) {
        console.log('获取IsAuthorization失败', res)

      }
    })
    wx.getStorage({
      key: 'IsVip',
      success: function (res) {
        console.log('获取isVip成功', res)
        app.globalData.IsVip = res.data        
      },
      fail: function (res) {
        console.log('获取isVip失败', res)

      }
    })
    wx.getStorage({
      key: 'Gold',
      success: function (res) {
        console.log('获取Gold成功', res)
        app.globalData.Gold = res.data
      },
      fail: function (res) {
        console.log('获取Gold失败', res)

      }
    })
    wx.getStorage({
      key: 'Experience ',
      success: function (res) {
        console.log('获取Experience 成功', res)
        app.globalData.Experience  = res.data
      },
      fail: function (res) {
        console.log('获取Experience失败', res)

      }
    })

  },
  gotoPK:function(){
    wx.navigateTo({
      url: '../../spokenPackage/pages/spokenPK/spokenPK',
    })
  },
  gotoDub: function () {
    wx.navigateTo({
      url: '../../dubbingPackage/pages/dubbingIndex/dubbingIndex',
    })
  },

  gotoMall: function () {
    wx.navigateTo({
      url: '../../malltaskPackage/pages/mall/mall',
    })
  },
  gotoTask:function(){
    // var taskList = JSON.stringify(this.data.taskList)
  //  console.log('143', taskList)
    wx.navigateTo({
      url: '../../malltaskPackage/pages/task/task',
      // url: '../../../malltaskPackage/pages/mallDetail/mallDetail?commodity=' + commodity
    })
  },
  comingSoon:function(){
  //  wx.showToast({
  //    title: '敬请期待...',
  //    duration:1500,
  //    icon:'loading',
  //   //  mask:'true'
  //  })
    wx.showModal({
      title: '新功能即将开放',
      showCancel: false,
      content: "敬请期待...",
    })
    
  },

  onShareAppMessage: function (res) {
    var that = this 
    if (res.from == 'button') {
     
    }
    return {
      title: '阿卡索英语角！',
      path: 'pages/tabBarPackage/index/index?shareID=' + 1 + '&UID=' + that.fata.UCUID,
      imageUrl: '',
      success: function (res) {
        console.log("转发成功");
      },
      fail: function (res) {
        // 转发失败?Data=' + self.data.wittd + '&openid=' + self.data.openId + '&Uid=' + 1
        console.log("转发失败");
      }
    }
  } 


})
